from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        circle1 = Circle(radius=1, color=RED,fill_opacity=0.5)  
        circle1.move_to([-47.5,27.5,0]) 
        circle2 = Circle(radius=1, color=YELLOW,fill_opacity=0.5)  
        circle2.move_to([-42.5,27.5,0])  
        circle3 = Circle(radius=1, color=GREEN,fill_opacity=0.5) 
        circle3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设平面区域D由}$ y=0,y=a,x=0,x=\sqrt{a^{2}+y^{2}}$"+
                   r"\text{围成}$( a > 0) .$"+
                   r"\text{求D绕y轴旋转所生成的旋转体的体积V,以及旋转体的表面积S}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        #镜头拉回
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(circle1),run_time=0.1)
        self.play(FadeIn(circle2),run_time=0.1)
        self.play(FadeIn(circle3),run_time=0.1)

        #题目向上移动
        self.play(text.animate().scale(0.8).move_to(UP*20)) 

        #文本

        text1 = Tex(r"$ V=\pi \int _{0}^{a}\left( a^{2}+y^{2}\right) dy=\dfrac{4}{3}\pi a^{3}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        text2 = Tex(r"$ S=2\pi \int _{0}^{a}x\sqrt{1+\dfrac{y^{1}}{x^{2}}}dy+\pi \left( 2a^{2}+a^{2}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text1,DOWN,buff=1)
        text3 = Tex(r"$ =2\pi \int _{0}^{a}\sqrt{a^{2}+2y^{2}}dy+3\pi a^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,DOWN,buff=1)
        text4 = Tex(r"$=\left[ \dfrac{\ln \left( \sqrt{2}+\sqrt{3}\right) }{\sqrt{2}}+\sqrt{3}+3\right] \pi a^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,DOWN,buff=1)
        
        self.play(Write(text1),Write(text2),Write(text3),Write(text4)) 

        #框框
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=40,height=15
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([0, -2.5, 0])
        self.play(Create(rectangle))
        #框框移动
        self.play(rectangle.animate().stretch(0.45,dim=0).stretch(0.3,dim=1).shift(1*LEFT+5*UP))
        #镜头拉近
        self.play(self.camera.frame.animate.scale(0.3).move_to([-1,2,0]))
        self.wait()